package io.github.echarts.render.coordinate;

import io.github.echarts.model.coordinate.CoordinateSystem;
import io.github.echarts.model.coordinate.CoordinateSystemType;
import io.github.echarts.model.coordinate.axis.AxisModel;
import io.github.echarts.model.coordinate.cartesian.CartesianCoordinateSystem;
import io.github.echarts.model.coordinate.cartesian.CartesianCoordinateTransformer;
import io.github.echarts.model.geometry.Rectangle;
import io.github.echarts.render.GraphicsRenderer;
import io.github.echarts.render.coordinate.cartesian.AxisRenderer;
import io.github.echarts.render.svg.SVGLayerType;

import java.awt.Font;

/**
 * 笛卡尔坐标系渲染器
 * 负责渲染笛卡尔坐标系，包括坐标轴、网格线等
 */
public class CartesianCoordinateSystemRenderer implements CoordinateSystemRenderer {
    
    /** 坐标轴渲染器 */
    private final AxisRenderer axisRenderer;
    
    /**
     * 构造函数
     */
    public CartesianCoordinateSystemRenderer() {
        this.axisRenderer = new AxisRenderer();
    }
    
    @Override
    public void render(CoordinateSystem coordinateSystem, GraphicsRenderer renderer) {
        if (!(coordinateSystem instanceof CartesianCoordinateSystem)) {
            throw new IllegalArgumentException("只能渲染笛卡尔坐标系");
        }
        
        CartesianCoordinateSystem cartesian = (CartesianCoordinateSystem) coordinateSystem;
        
        // 获取坐标系布局区域
        Rectangle layoutArea = cartesian.getLayoutArea();
        
        // 渲染网格背景
        renderGridBackground(layoutArea, renderer);
        
        // 获取坐标转换器
        CartesianCoordinateTransformer transformer = cartesian.getTransformer();
        
        // 渲染坐标轴 - 使用专门的坐标轴渲染器
        renderAxesWithRenderer(cartesian, renderer, transformer);
    }
    
    @Override
    public CoordinateSystemType getSupportedType() {
        return CoordinateSystemType.CARTESIAN;
    }
    
    /**
     * 渲染网格背景
     * 
     * @param layoutArea 布局区域
     * @param renderer 图形渲染器
     */
    private void renderGridBackground(Rectangle layoutArea, GraphicsRenderer renderer) {
        // 渲染背景矩形 - 使用对象式API
        renderer.drawRect(
            (int)layoutArea.getX(), 
            (int)layoutArea.getY(), 
            (int)layoutArea.getWidth(), 
            (int)layoutArea.getHeight(),
            true,  // 填充
            "#f7f7f7", // 默认浅灰色背景
            null  // 无边框
        );
    }
    
    /**
     * 使用专门的坐标轴渲染器渲染坐标轴
     * 
     * @param cartesian 笛卡尔坐标系
     * @param renderer 图形渲染器
     * @param transformer 坐标转换器
     */
    private void renderAxesWithRenderer(CartesianCoordinateSystem cartesian, 
                                     GraphicsRenderer renderer,
                                     CartesianCoordinateTransformer transformer) {
        // 获取布局区域
        Rectangle area = cartesian.getLayoutArea();
        
        // 获取X轴和Y轴模型
        AxisModel xAxisModel = cartesian.getXAxisModel();
        AxisModel yAxisModel = cartesian.getYAxisModel();
        
        // 将控制权交给专门的坐标轴渲染器
        if (xAxisModel != null) {
            axisRenderer.renderXAxis(renderer, xAxisModel, area, transformer);
        }
        
        if (yAxisModel != null) {
            axisRenderer.renderYAxis(renderer, yAxisModel, area, transformer);
        }
    }
} 